//
//  CalendarButton.m
//  HarmonizePub
//
//  Created by Haven on 16/9/20.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "CalendarButton.h"
#import "UIView+extension.h"
#import "Config.h"

#define calendarWidth 7

@interface CalendarButton()
@property (nonatomic, assign) CGFloat titleH;

@end

@implementation CalendarButton
- (UILabel *)lable
{
    if (_lable == nil) {
        _lable = [[UILabel alloc] init];
    }
    return _lable;
}


/**
 *  类方法创建按钮
 */
+ (instancetype)calendarButton
{
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 设置粗体
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        //设置按钮大小
        self.width = [UIScreen mainScreen].bounds.size.width / calendarWidth;
        self.height = 80;
        
        //设置图片居中
        self.imageView.contentMode = UIViewContentModeCenter;
        self.titleLabel.contentMode = UIViewContentModeCenter;

        // 添加显示日期的Lable
        self.lable.frame = CGRectMake(0, 10, self.width - 10, self.height - self.titleH - 30);
        
        self.lable.textAlignment = NSTextAlignmentCenter;
        
        [self addSubview:self.lable];
    }
    return self;
}

// 修改title属性
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleX = 0;
    CGFloat titleY = 0;
    CGFloat titleW = self.width;
    CGFloat titleH = 0;
    // 宽度随文字变化
    NSDictionary *dict = @{
                           NSFontAttributeName : [UIFont systemFontOfSize:13.0]
                           };
    titleH = [self.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options: NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size.height;
    self.titleH = titleH;
    return CGRectMake(titleX, titleY, titleW, titleH);
}
// 修改Image属性
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageX = 0;
    CGFloat imageW = self.width;
    CGFloat imageH = self.height - self.titleH;
    CGFloat imageY = self.titleH;
    return CGRectMake(imageX, imageY, imageW, imageH);
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    if (selected)
        self.lable.textColor = [UIColor whiteColor];
    else
        self.lable.textColor = [UIColor blackColor];
}

- (void)setEnabled:(BOOL)enabled {
    [super setEnabled:enabled];
    if (enabled) {
        if (!self.selected) {
            self.lable.textColor = [UIColor blackColor];
        }
    } else {
        self.lable.textColor = [UIColor whiteColor];
    }
}

@end
